import request from '@/utils/request'
import defaultSettings from '@/settings'
const Base64 = require('js-base64').Base64

export const apiUrlConfig = {
  queryUrl: `${defaultSettings.ctx.authorityApp}/s/deptInfo/search`,
  exportUrl: `${defaultSettings.ctx.authorityApp}/s/deptInfo/search`,
  delUrl: `${defaultSettings.ctx.authorityApp}/s/deptInfo/remove`,
  templateUrl: `${defaultSettings.ctx.authorityApp}/s/deptInfo/exportDeptLineTemplate`,
  importUrl: `${defaultSettings.ctx.authorityApp}/s/deptInfo/importDeptFile`,
}

// index 页面初始化
export function indexInit() {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/deptInfoInit`,
    method: 'post'
  })
}

// 导出
export function exportAll(json,data) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/searchAll`,
    headers: {
      'exportType': 'XLSX',
      'HT-super-export-meta': Base64.encode(JSON.stringify(json)),
    },
    method: 'post',
    data
  })
}

// 查询列表
export function search(query) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/searchToTree`,
    method: 'post',
    data: query
  })
}

export function findDeptInfoById(id) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/findDeptInfoById`,
    method: 'get',
    params: {
      id: id
    }
  })
}
// 机构管理页面树数据加载
export function navigationTree() {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/navigationTreeVue`,
    method: 'post',
  })
}

// 删除组织机构
export function delDeptInfo(data) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/remove?id=${data}`,
    method: 'get'
  })
}

//组织机构树形组建
// export function deptTreeData() {
//   return request({
//     url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/queryDeptTreeDataNewVue`,
//     method: 'get'
//   })
// }

// 新增组织机构
export function addDeptInfo(data) {
  return request({
    headers: {
      'Content-Type' : 'application/json'
    },
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/add`,
    method: 'post',
    data: data
  })
}

// 修改组织机构
export function updateDeptInfo(data) {
  return request({
    headers: {
      'Content-Type' : 'application/json'
    },
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/edit`,
    method: 'post',
    data: data
  })
}

// 校验组织机构编码
export function checkDeptCode(data) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/checkDeptCodeUnique`,
    method: 'get',
    params:data
  })
}

// 校验组织机构名称
export function checkDeptName(data) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/checkDeptNameUnique`,
    method: 'get',
    params:data
  })
}
//组织机构树形组建
export function deptTreeData(userId,deptId,deptCode,tenantCode,name) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/queryDeptTreeDataVue?userId=`+userId+`&deptId=`+deptId+`&deptCode=`+deptCode+`&tenantCode=`+tenantCode+`&name=`+name,
    method: 'get'
  })
}

// 查询租户组织机构下拉树
export function tenantDeptTree(userId,deptId,deptCode,tenantCode,name) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/queryTenantDeptTreeData?userId=`+userId+`&deptId=`+deptId+`&deptCode=`+deptCode+`&tenantCode=`+tenantCode+`&name=`+name,
    method: 'get',
  })
}
// 通过机构id查询用户的上级机构名称
export function selectDeptNameById(deptId) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/selectDeptNameByIdVue?deptId=`+deptId,
    method: 'get',
  })
}
// 查询租户组织机构下拉树
export function tenantDeptTreeDisabled(deptId,deptCode,tenantCode) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptInfo/queryTenantDeptTreeDataDisabled?deptId=`+deptId+`&deptCode=`+deptCode+`&tenantCode=`+tenantCode,
    method: 'get',
  })
}

// 租户是否禁用,true:禁用,false:不禁用
export function deptSelectTree(searchText = '',tenantSelect = false,tenantCode = '',disabledCode = '') {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptTree/queryDeptTreeDataByLevel?name=${searchText}&tenantSelect=${tenantSelect}&tenantCode=${tenantCode}&disabledCode=${disabledCode}`,
    method: 'get',
  })
}

export function deptSelectTreeForClick(deptCode,currentEditDeptCode = '',nodeType) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptTree/queryDeptChildrenDataByDeptCode?deptCode=${deptCode}&disabledCode=${currentEditDeptCode}&nodeType=${nodeType}`,
    method: 'get',
  })
}

export function queryDeptDataByDeptCode(code) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/deptTree/queryDeptDataByDeptCode?deptCode=${code}`,
    method: 'get',
  })
}
